#pragma once 
#include <vector> 
#include <optixu/optixu_math_namespace.h>
#include "device_util.h"
using namespace std;


class KD {
public: 
	KD (PhotonRec *photons, 
		optix::uint width, 
		optix::uint height, 
		optix::float3 min_box, 
		optix::float3 max_box); 

	void store( PhotonRec *buffer );

	void gatherCPU( ViewRayData* hit_buffer, float* output, int width, int height, int plw, int plh ); 
	
	static int computeSize(optix::uint pl_width, optix::uint pl_height);

private:

	void build( vector<PhotonRec> &pts, 
				int ind,
				int start, 
				int last, 
				int depth, 
				// To find the longest dimension
				optix::float3 bbox_min, 
				optix::float3 bbox_max );
	 
	int size;
	int maxPhotons;

	vector<PhotonRec> kd;   	
	
	int getMedian(optix::float3 &bbmin, optix::float3 &bbmax, vector<PhotonRec> &pts, int start, int last); 

	int getSplitDim(optix::float3 &bbmin, optix::float3 &bbmax);	 
};